home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / dpmigcc5.zip / RSX / SOURCE / STATEMX.C < prev    next >
C/C++ Source or Header  |  1994-05-27  |  4KB  |  132 lines

  1. /*****************************************************************************
  2.  * FILE: statemx.c                                                           *
  3.  *                                         *
  4.  * DESC:                                     *
  5.  *      - stat, fstat functions                                              *
  6.  *                                         *
  7.  * Copyright (C) 1993,1994                             *
  8.  *    Rainer Schnitker, Heeper Str. 283, 33607 Bielefeld             *
  9.  *    email: rainer@mathematik.uni-bielefeld.de                 *
  10.  *                                         *
  11.  *****************************************************************************/
  12.  
  13. #include "DPMI.H"
  14. #include "RMLIB.H"
  15. #include "TIMEDOS.H"
  16. #include "STATEMX.H"
  17.  
  18. #define ACC(x) (((x)>>6)*0111)
  19.  
  20. static long ino = 0x10000;
  21.  
  22. int sys_stat(char *filename, struct stat * statbuf)
  23. {
  24.     struct find_t find;
  25.     unsigned long gmt;
  26.  
  27.     if (filename == NULL)
  28.     return -1;
  29.  
  30.     /* find : dir, system, hidden */
  31.     if (rm_findfirst(filename, 0x16, &find) == -1) {
  32.     /* error: must be a root dir */
  33.     char path[260];
  34.     unsigned olddrive, newdrive;
  35.     int ret;
  36.  
  37.     olddrive = rm_getdrive();
  38.     if (filename[1] == ':') {
  39.         newdrive = (unsigned) *filename;
  40.         if (newdrive >= 'a')
  41.         newdrive -= 0x20;
  42.         if (newdrive < 'A' || newdrive > 'Z')
  43.         return -1;
  44.         newdrive -= 'A';
  45.         if (newdrive != olddrive)
  46.         rm_setdrive(newdrive);
  47.     } else
  48.         newdrive = olddrive;
  49.     rm_getcwd(0, path);
  50.     ret = rm_chdir(filename);
  51.     rm_chdir(path);        /* restore path */
  52.     if (newdrive != olddrive)    /* restore drive */
  53.         rm_setdrive(olddrive);
  54.     if (ret == -1)
  55.         return -1;        /* no root, error */
  56.     statbuf->st_mode = S_IFDIR | ACC(S_IREAD | S_IWRITE | S_IEXEC);
  57.     statbuf->st_size = 0L;
  58.     statbuf->st_dev = (long) newdrive;
  59.     statbuf->st_attr = 0;
  60.     gmt = 0;
  61.     } else {
  62.     struct file_time ft;
  63.  
  64.     statbuf->st_attr = (long) find.attrib;
  65.     if (find.attrib & 0x10) {    /* directory */
  66.         statbuf->st_mode = S_IFDIR | ACC(S_IREAD | S_IWRITE | S_IEXEC);
  67.         statbuf->st_size = 0L;
  68.     } else {
  69.         statbuf->st_mode = S_IFREG;    /* file */
  70.         statbuf->st_mode |= ACC(S_IREAD);
  71.         if (!(find.attrib & 0x1))    /* test read only */
  72.         statbuf->st_mode |= ACC(S_IWRITE);    /* Read-write access */
  73.         statbuf->st_size = (DWORD) find.size_hi << 16 | find.size_lo;
  74.     }
  75.     ft.ft_date = find.wr_date;
  76.     ft.ft_time = find.wr_time;
  77.     gmt = filetime2gmt(&ft);
  78.     if (find.name[1] == ':')
  79.         statbuf->st_dev = (long) (find.name[0] - 'A');
  80.     else
  81.         statbuf->st_dev = rm_getdrive();
  82.     }
  83.     /* all */
  84.     statbuf->st_atime = gmt;
  85.     statbuf->st_ctime = gmt;
  86.     statbuf->st_mtime = gmt;
  87.     statbuf->st_rdev = statbuf->st_dev;
  88.     statbuf->st_uid = 0L;
  89.     statbuf->st_gid = 0L;
  90.     statbuf->st_ino = ino++;
  91.     statbuf->st_nlink = 1L;
  92.     statbuf->st_reserved = 0L;
  93.     return 0;
  94. }
  95.  
  96. int sys_fstat(int handle, struct stat * statbuf)
  97. {
  98.     struct file_time ft;
  99.     unsigned long gmt;
  100.     int info;
  101.  
  102.     if ((info = rm_ioctl_getattr(handle)) == -1)
  103.     return -1;
  104.  
  105.     if (!(info & 0x80)) {    /* file */
  106.     long oldpos;
  107.     statbuf->st_mode = S_IFREG;
  108.     oldpos = rm_lseek(handle, 0L, 1);
  109.     statbuf->st_size = rm_lseek(handle, 0L, 2);
  110.     rm_lseek(handle, oldpos, 0);
  111.     statbuf->st_dev = 0;
  112.     } else {            /* device */
  113.     statbuf->st_mode = S_IFCHR;
  114.     statbuf->st_size = 0L;
  115.     statbuf->st_dev = (long) (info & 0x3F);
  116.     }
  117.     rm_getftime(handle, &ft.ft_date, &ft.ft_time);
  118.     gmt = filetime2gmt(&ft);
  119.     statbuf->st_atime = gmt;
  120.     statbuf->st_ctime = gmt;
  121.     statbuf->st_mtime = gmt;
  122.     statbuf->st_mode |= ACC(S_IREAD | S_IWRITE);
  123.     statbuf->st_uid = 0L;
  124.     statbuf->st_gid = 0L;
  125.     statbuf->st_rdev = statbuf->st_dev;
  126.     statbuf->st_nlink = 1L;
  127.     statbuf->st_ino = ino++;
  128.     statbuf->st_attr = 0L;
  129.     statbuf->st_reserved = 0L;
  130.     return 0;
  131. }
  132.